﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExternalConnections.NumericTextBox
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExternalConnections.Res;
using System;
using System.Globalization;
using System.Windows.Forms;


namespace DnProcessSimulateCommands.ExternalConnections;

public class NumericTextBox : TextBox
{
  private bool allowSpace;
  private bool isPortNumericBox = true;
  private ToolTip _portToolTip = new ToolTip();

  public new event EventHandler Enter;

  protected override void OnEnter(EventArgs e)
  {
    if (this.Enter == null)
      return;
    this.Enter((object) this, e);
  }

  protected override void OnKeyPress(KeyPressEventArgs e)
  {
    base.OnKeyPress(e);
    NumberFormatInfo numberFormat = CultureInfo.CurrentCulture.NumberFormat;
    string decimalSeparator = numberFormat.NumberDecimalSeparator;
    string str1 = numberFormat.NumberGroupSeparator;
    if (str1 == ' '.ToString())
      str1 = " ";
    string str2 = e.KeyChar.ToString();
    if (char.IsDigit(e.KeyChar))
    {
      this.verifyPortNumber(e);
    }
    else
    {
      if (str2.Equals(decimalSeparator) || str2.Equals(str1) || e.KeyChar == '\b')
        return;
      if (e.KeyChar == '\r')
        this.OnEnter((EventArgs) e);
      else
        e.Handled = true;
    }
  }

  protected void verifyPortNumber(KeyPressEventArgs e)
  {
    if (!this.isPortNumericBox || ushort.TryParse(this.Text.Remove(this.SelectionStart, this.SelectionLength).Insert(this.SelectionStart, e.KeyChar.ToString()), out ushort _))
      return;
    e.Handled = true;
    this._portToolTip.Show(CUiECExternalConnectionsResourceTable.OPC_UA_PORT_TOOLTIP, (IWin32Window) this, 0, this.ClientRectangle.Bottom + 4, 3000);
  }

  public int IntValue => int.Parse(this.Text);

  public Decimal DecimalValue => Decimal.Parse(this.Text);

  public bool AllowSpace
  {
    set => this.allowSpace = value;
    get => this.allowSpace;
  }

  public bool IsPortNumericBox
  {
    set => this.isPortNumericBox = value;
    get => this.isPortNumericBox;
  }
}
